
function formatNumber(n) {
    const str = n.toString()
    return str[1] ? str : `0${str}`
  }
  
  
  
  function formatNumber(n) {
    const str = n.toString()
    return str[1] ? str : `0${str}`
  }
  
  function formatTwo(n) {
    if (n >= 10) return n
    else return '0' + n
  }

  export function formatTime2(number, format) {
  
    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];
  
    var date = new Date(number);
    returnArr.push(date.getFullYear());
    returnArr.push(formatNumber(date.getMonth() + 1));
    returnArr.push(formatNumber(date.getDate()));
  
    returnArr.push(formatNumber(date.getHours()));
    returnArr.push(formatNumber(date.getMinutes()));
    returnArr.push(formatNumber(date.getSeconds()));
  
    for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
  }
  /**
  * 根据日期获取月份
  */
  export function getMonthStr(str) {
    //获取返回年月份
    let date = new Date(str);
    let month = str.substr(4, 2)
    let year = str.substr(0, 4)
    //获取当前年月份
    let nowDate = new Date();
    let nowMonth = nowDate.getMonth() + 1
    let nowYear = nowDate.getFullYear()
    if (nowYear == year && nowMonth != month) {
      return month + '月'
    } else if (nowYear == year && nowMonth == month) {
      return '本月'
    } else {
      return year + '年' + month + '月'
    }
  }
  
  /**
  * 获取年月日
  */
  export function formatDate(date) {
    var date = new Date(date);
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
  
    return [year, month, day].map(formatNumber).join('-')
  }
  
  export function formatTime(date) {
    var date = new Date(date);
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
  
    return [year, month, day].map(formatNumber).join('-') + '   ' + [hour, minute, second].map(formatNumber).join(':')
  }
  
  export default {
    formatNumber,
    formatTime
  }
  
  /**
  * 根据日期获取年月
  */
  export function getMonth() {
    let date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    return year + '-' + month
  }
/**
 * /**value 格式为13位unix时间戳
 * 10位unix时间戳可通过value*1000转换为13位格式
 */
export function formatTimestamp(date, fmt) {
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + ''
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
      }
    }
    return fmt
  }
  
  function padLeftZero(str) {
    return ('00' + str).substr(str.length)
  }

  /**
* 201801 ->  2018-01
*/
export function translateDate(date) {
    let month = date.substr(4, 2)
    let year = date.substr(0, 4)
    return year + '-' + month
  }


  /**
* 判断年龄范围(精确到月)
*/
export function checkAge(birth) {
    //获取当前年月
    let date1 = new Date()
    let nowYear = date1.getFullYear()
    let nowMonth = date1.getMonth() + 1
    //获取生日
    let array = birth.split('-')
    let birthYear = array[0]
    let birthMonth = array[1]
    //比较
    if (nowYear > birthYear && nowMonth >= birthMonth) {
      return nowYear - birthYear
    } else if (nowYear > birthYear && nowMonth < birthMonth) {
      return nowYear - birthYear - 1
    } else {
      return 0
    }
  }
  
  /**
  * 判断多少月范围
  */
  export function checkMonth(begin, end) {
    //获取开始日期
    let array1 = begin.split('-')
    let date1Year = array1[0]
    let date1Month = array1[1]
    //结束日期
    let array2 = end.split('-')
    let date2Year = array2[0]
    let date2Month = array2[1]
    //比较  2017-11  2018-12
    if (date2Year > date1Year && date2Month >= date1Month) {
      return (date2Year - date1Year) * 12 + (date2Month - date1Month) + 1
    } else if (date2Year > date1Year && date2Month < date1Month) {
      return (date2Year - date1Year) * 12 - (date1Month - date2Month) + 1
    } else {
      return false
    }
  }
  